var fs = require('fs');

function strMapToObj(strMap) {
  let obj = Object.create(null);
  for (let [k,
    v]of strMap) {
    obj[k] = v;
  }
  return obj;
}

function strMapToJson(strMap) {
  return JSON.stringify(strMapToObj(strMap));
}

var Key = [];
var Value = [];

var readline = require('linebyline'),
  rl = readline('/Users/eh/Documents/mywork/abc/3M综合征1型.txt');
rl.on('line', function(line, lineCount, byteCount) {
  var regex = /【(.*)】/;
  var regex2 = /^[^【]|^$/;

  if (line.match(regex)) {
    var keys = line.match(regex);
    Key.push(keys.input.toString())
  } else if (line.match(regex2)) {
    var values = line.match(regex2);
    Value.push(values.input.toString());
  }

}).on('error', function(e) {
  // console.log(e)
  // something went wrong
});

var myMap = new Map();
fs.writeFile("./data.json", Value, (err) => {
  if (err)
    throw err;
  console.log('It\'s saved!');
  console.log(Value.length);
  console.log(Key.length);
  for (var i = 0; i < Value.length; i++) {
    myMap.set(Key[i], Value[i]);
    var a = strMapToJson(myMap);

  }
  console.log(a);

});
